/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.TbMpWallet;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.TbMpWalletService;
import com.thinkgem.jeesite.modules.sys.service.UserService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 代理商Controller
 *
 * @version 2016-10-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/agent")
public class TbAgentController extends BaseController {

/*	@Autowired
	private TbAgentService tbAgentService;*/
	@Autowired
	private UserService userService;
	@Autowired
	private TbMpWalletService tbMpWalletService;


	/**
	 * 根据UID查询代理商 jyy
	 *  登陆对象的id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "byUIDAgent")
	public String queryByUIDAgentAndArea(Model model){
		String id = UserUtils.getUser().getId();// 获取当前用户id
		User user = userService.queryUser(id);
		model.addAttribute("user",user);
		String str = userService.queryArea(user);
		model.addAttribute("qy",str);
		return "modules/sys/agent";
	}

	/**
	 * 查询所有代理商 jyy
	 * lishuai update 2016-11-03 23:30
	 */
	@RequestMapping(value = "queryAgentList")
	public String queryList(User userAgent, HttpServletRequest request, HttpServletResponse response, Model model){
		if(userAgent==null){
			userAgent = new User();
		}
		Page<User> userAgentPage = userService.findAgentByPage(new Page<User>(request, response), userAgent);

		List<User> users = userAgentPage.getList();
		List<String> listMpWallet = new ArrayList<String>();
		for(User us: users){
			listMpWallet.add(us.getId());
		}
		if(listMpWallet.size()!=0){
			List<TbMpWallet> list = tbMpWalletService.selectByUid(listMpWallet);
			for(User us: users){
				for(TbMpWallet mq: list){
					if(us!=null & mq!=null){
						if(us.getId().equals(mq.getUid())){
							us.setEmail(mq.getTotalMoney()+"");
						}
					}
				}
			}
			List<Map<String,String>> areasList = userService.selectAreasByUserId(listMpWallet);
			for(User us: users) {
				for (Map<String, String> areas : areasList) {
					if(us.getId().equals(areas.get("id"))){
						us.setOldLoginName(areas.get("areas"));
					}
				}
			}
		}

		model.addAttribute("userAgent",userAgent);
		model.addAttribute("userAgentPage",userAgentPage);
		model.addAttribute("page",userAgentPage);
		return "modules/sys/agentList";
	}

	// 更改代理商信息
	@RequestMapping(value = "updateAgent")
	public String updateAgent(User user){

		userService.saveUpdateAgent(user);
		return "redirect:" + adminPath + "/sys/agent/byUIDAgent";
	}


	// 更改代理商状态
	@RequestMapping(value = "updateAgentStatus")
	public String updateAgentStatus(HttpServletRequest request){
		String userId = request.getParameter("id");
		userService.updateAgentStatus(userId);
		return "redirect:" + adminPath + "/sys/agent/queryAgentList";
	}
}
